# models.py
import datetime

from django.contrib.auth.models import User  # 使用系统内置用户系统
from django.db import models

from KeHu.models import KeHu
from tinymce.models import HTMLField


class ChanpinLeibie(models.Model):
    """产品类别"""
    name = models.CharField(verbose_name='类别名称', max_length=100, default="")
    image = models.ImageField(verbose_name='图片', upload_to='uploads/', blank=True, null=True)
    xianshi_choices = (
        (0, '否'),
        (1, '是'),
    )
    xiaolei_choices = (
        (1, "自考"),
        (2, "成人高考"),
        (3, "开放大学"),
        (4, "特种作业证"),
        (5, "资格等级证"),
    )
    if_xiaolei = models.IntegerField(verbose_name='产品小类', default=0, choices=xiaolei_choices,blank=True,null=True)
    if_xianshi = models.IntegerField(verbose_name='是否显示', default=1, choices=xianshi_choices,blank=True, null=True)
    description = models.TextField(verbose_name='描述', blank=True, null=True)

    def __str__(self):
        return self.name + ' - ' + self.get_xiaolei_display()

    def get_xiaolei_display(self):
        # 创建一个字典来映射整数值到显示名称
        xiaolei_dict = dict(self.xiaolei_choices)
        return xiaolei_dict.get(self.if_xiaolei, ' ')

    class Meta:
        verbose_name = '产品类别'
        verbose_name_plural = '产品类别'


class Chanpin_gys(models.Model):
    """产品供应商"""
    name = models.CharField(verbose_name='供应商名称', max_length=22, default="")
    lxr = models.CharField(verbose_name='联系人', max_length=22, null=True)
    tel = models.CharField(verbose_name='联系电话', max_length=22, null=True)
    dizhi = models.CharField(verbose_name='联系地址', max_length=22, null=True)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '供应商'
        verbose_name_plural = '产品供应商'


class Chanpin(models.Model):
    """产品"""
    title = models.CharField(verbose_name='产品名称', max_length=22, default="")
    jiage = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='产品价格', null=True)
    jiage_dijia = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='产品底价', null=True)
    leibie = models.ForeignKey(ChanpinLeibie, on_delete=models.CASCADE, verbose_name='产品类别',null=True)
    gys = models.ForeignKey(Chanpin_gys, on_delete=models.CASCADE, verbose_name='产品供应商')
    beizhu = HTMLField(verbose_name='备注', blank=True, null=True)
    fujian = models.FileField(verbose_name='附件', upload_to='uploads/', blank=True, null=True)
    cbj=models.DecimalField(max_digits=10, decimal_places=2, verbose_name='成本价', null=True)

    # 注意：由于ChanpinFenlei是抽象基类，你需要确定如何在实际应用中处理这个关系，可能需要使用多态关系或者其他方式。

    def __str__(self):
        return self.title +'-'+self.gys.name+'-'+self.leibie.name

    class Meta:
        verbose_name = '产品'
        verbose_name_plural = '产品'


class Dingdan(models.Model):
    ddh = models.CharField(verbose_name='订单号', max_length=22, default="")
    ddtime = models.DateTimeField(verbose_name='订单创建时间', auto_now=False, null=True,default=datetime.datetime.now)
    dduser = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='订单归属', default='',null=True)
    kehu = models.ForeignKey(KeHu, on_delete=models.CASCADE, verbose_name='订单客户', default='')
    ddchanpin = models.ManyToManyField(Chanpin, verbose_name='订单产品')
    ddjine = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单金额', null=True)
    kaipiao_choices = (
        (0, '不需要'),
        (1, '需要'),
    )
    kaipiaoif=models.IntegerField(verbose_name='开票是否', default=0,choices=kaipiao_choices)
    ddzhuangtai_choices= (
        ('已完成', '已完成'),
        ('已取消', '已取消'),
    )

    #ddzhuangtai=models.CharField(verbose_name='订单状态', max_length=22, default="已完成",choices=ddzhuangtai_choices)


    def __str__(self):
        return self.ddh

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '用户订单'


# class  set_xueyuan(models.Model):
#     xueyuan_name = models.CharField(verbose_name='学院名称', max_length=22, default="")
#     xueyuan_image = models.ImageField(verbose_name='学院图片', upload_to='uploads/', blank=True, null=True)
#     xueyuan_description = models.TextField(verbose_name='学院简介', blank=True, null=True)
#
#     def __str__(self):
#         return self.xueyuan_name
#
#     class Meta:
#         verbose_name = '学院'
#         verbose_name_plural = '学院管理'